/**
 * Created by liufeng6, 2016-09-27
 * 认证比对app页面Service
 */
angular.module('rzbd.service',[])
  .service('rzbdService',['$q','$http','$timeout','$ionicPopup','$ionicLoading','$cordovaBarcodeScanner','appItfAopService',function($q,$http,$timeout,$ionicPopup,$ionicLoading,$cordovaBarcodeScanner,appItfAopService){

    var params={};
    return{
      testFunc:function(){
        document.getElementById("idAvatarSrc").innerText= "12312113123133132";
      },
      getParams:function(){
        return params;
      },
      clearIdCardInfo:function(){
        params.idCardInfo = {};
      },
      //插入日志
      insertLog:function(param)
      {
        return appItfAopService.do_insertLog_req(param);
      },


      readIdCardBlueTooth :function(param){
        console.debug("rzbd readIdCardBlueTooth: "+param);
        $ionicLoading.show({
        });
        //alert("before blue tooth");
        bluetooth.sr_lydk(param,function(success){
          console.log("rzbd readIdCardBlueTooth bluetooth success: "+success);
          //alert("blue tooth ok:"+success);
            $ionicLoading.hide();
            $timeout(function(){
              params.idCardInfo=angular.fromJson(success);
              //alert(params.idCardInfo.avatar);
              //document.getElementById("idAvatarSrc").innerText = params.idCardInfo.avatar;
              var avatarSrc = bytesToBase64(params.idCardInfo.avatar);
              params.idCardInfo.avatar64 = avatarSrc;
              document.getElementById("imgIdPic").src = "data:image/jpeg;base64," + avatarSrc;
              document.getElementById("txtIdCardNo").value = params.idCardInfo.cardNo;
              document.getElementById("txtIdName").value = params.idCardInfo.name;
              //document.getElementById("idAvatarSrc").innerText = avatarSrc;
            },1000);
          },function(error){
            $ionicLoading.hide();
            //alert("blue tooth fail:");
            params.idCardInfo = {};
            var alertPopup=$ionicPopup.alert({
              title:'读取失败',
              template:angular.toJson(error),
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred' // String (默认: 'button-positive')。OK按钮的类型。
            });
          }
        );
        return params.idCardInfo;
      },

      submitRzbd:function(param){
        //alert(param.itemId+" sUbmit");
        return appItfAopService.do_rzbdSubmit_req(param);
      },

    }

    function bytesToBase64(bytes){
      var arrChars = new Array();
      for ( var i=0; i<bytes.length; i++ ){
        var c = String.fromCharCode(bytes[i]);
        arrChars.push(c);
      }
      return stringToBase64(arrChars.join(""));
    }

    function stringToBase64(strBytes){
      var table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
      var res = [];
      var len = strBytes.length;
      var i = 0;
      while (i < len) {
        var c1 = strBytes.charCodeAt(i++) & 0xFF;
        res.push(table[c1 >> 2]);
        // 需要补2个=
        if (i == len) {
          res.push(table[(c1 & 0x3) << 4]);
          res.push('==');
          break;
        }
        var c2 = strBytes.charCodeAt(i++);
        // 需要补1个=
        if (i == len) {
          res.push(table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
          res.push(table[(c2 & 0x0F) << 2]);
          res.push('=');
          break;
        }
        var c3 = strBytes.charCodeAt(i++);
        res.push(table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
        res.push(table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
        res.push(table[c3 & 0x3F]);
      }

      return res.join('');
    }

  }]);
